home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / dwmainc.cpp < prev    next >
C/C++ Source or Header  |  1997-05-29  |  5KB  |  219 lines

  1. /* Copyright (C) 1996, 1997, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19.  
  20. // dwmainc.cpp
  21. // Ghostscript DLL loader for Windows 95/NT
  22. // For WINDOWCOMPAT (console mode) application
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <shellapi.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <dos.h>
  31. extern "C" {
  32. #include "gscdefs.h"
  33. #define GSREVISION gs_revision
  34. #include "gsdll.h"
  35. }
  36. #include "dwmain.h"
  37. #include "dwdll.h"
  38.  
  39. #if defined(_MSC_VER) && defined(__WIN32__)
  40. #define _export
  41. #endif
  42.  
  43. /* public handles */
  44. HINSTANCE ghInstance;
  45.  
  46. const char *szDllName = "GSDLL32.DLL";
  47.  
  48.  
  49. int FAR _export gsdll_callback(int message, char FAR *str, unsigned long count);
  50.  
  51. // the Ghostscript DLL class
  52. gsdll_class gsdll;
  53.  
  54. char start_string[] = "systemdict /start get exec\n";
  55.  
  56. // program really starts at WinMain
  57. int
  58. new_main(int argc, char *argv[])
  59. {
  60. typedef char FAR * FARARGV_PTR;
  61. int rc;
  62.  
  63.     // load DLL
  64.     if (gsdll.load(ghInstance, szDllName, GSREVISION)) {
  65.     char buf[256];
  66.     gsdll.get_last_error(buf, sizeof(buf));
  67.     fputs(buf, stdout);
  68.     return 1;
  69.     }
  70.  
  71.     // initialize the interpreter
  72.     rc = gsdll.init(gsdll_callback, (HWND)NULL, argc, argv);
  73.     if (rc == GSDLL_INIT_QUIT) {
  74.         gsdll.unload();
  75.     return 0;
  76.     }
  77.     if (rc) {
  78.     char buf[256];
  79.     gsdll.get_last_error(buf, sizeof(buf));
  80.     fputs(buf, stdout);
  81.         gsdll.unload();
  82.     return rc;
  83.     }
  84.  
  85.     // if (!batch)
  86.     gsdll.execute(start_string, strlen(start_string));
  87.     
  88.     gsdll.unload();
  89.  
  90.     return 0;
  91. }
  92.  
  93.  
  94. #if defined(_MSC_VER)
  95. /* MSVC Console EXE needs main() */
  96. int
  97. main(int argc, char *argv[])
  98. {
  99.     /* need to get instance handle */
  100.     ghInstance = GetModuleHandle(NULL);
  101.  
  102.     return new_main(argc, argv);
  103. }
  104. #else
  105. /* Borland Console EXE needs WinMain() */
  106. #pragma argsused  // ignore warning about unused arguments in next function
  107.  
  108. int PASCAL 
  109. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  110. {
  111. #if defined(_MSC_VER)    /* MSC doesn't give us _argc and _argv[] so ...   */
  112. #define MAXCMDTOKENS 128
  113.     int    _argc=0;
  114.     LPSTR    _argv[MAXCMDTOKENS];
  115.     LPSTR    p, q;
  116. #ifdef __WIN32__
  117.     _argv[_argc] = "gswin32.exe";
  118. #else
  119.     _argv[_argc] = "gswin.exe";
  120. #endif
  121.     // Parse command line handling quotes.
  122.     p = lpszCmdLine;
  123.     while (*p) {
  124.         // for each argument
  125.         while ((*p) && (*p == ' '))
  126.         p++;    // skip over leading spaces
  127.         if (*p == '\042') {
  128.            p++;        // skip "
  129.            q = p;
  130.            // scan to end of argument
  131.            // doesn't handle embedded quotes
  132.            while ((*p) && (*p != '\042'))
  133.             p++;
  134.            _argv[++_argc] = q;
  135.            if (*p)
  136.             *p++ = '\0';
  137.         }
  138.         else if (*p) {
  139.            // delimited by spaces
  140.            q = p;
  141.            while ((*p) && (*p != ' '))
  142.             p++;
  143.            _argv[++_argc] = q;
  144.            if (*p)
  145.             *p++ = '\0';
  146.         }
  147.     }
  148.     _argv[++_argc] = (LPSTR)NULL;
  149. #endif
  150.  
  151.     if (hPrevInstance) {
  152.         fputs("Can't run twice", stdout);
  153.         return FALSE;
  154.     }
  155.  
  156.     /* copy the hInstance into a variable so it can be used */
  157.     ghInstance = hInstance;
  158.  
  159.     return new_main(_argc, _argv);
  160. }
  161. #endif
  162.  
  163.  
  164. int
  165. read_stdin(char FAR *str, int len)
  166. {
  167. int ch;
  168. int count = 0;
  169.     while (count < len) {
  170.     ch = fgetc(stdin);
  171.     if (ch == EOF)
  172.         return count;
  173.     *str++ = ch;
  174.     count++;
  175.     if (ch == '\n')
  176.         return count;
  177.     }
  178.     return count;
  179. }
  180.  
  181.  
  182. int FAR _export
  183. gsdll_callback(int message, char FAR *str, unsigned long count)
  184. {
  185. char buf[256];
  186.     switch (message) {
  187.     case GSDLL_POLL:
  188.         // Don't check message queue because we don't
  189.         // create any windows.
  190.         // May want to return error code if abort wanted
  191.         break;
  192.     case GSDLL_STDIN:
  193.         return read_stdin(str, count);
  194.     case GSDLL_STDOUT:
  195.         fwrite(str, 1, count, stdout);
  196.         fflush(stdout);
  197.         return count;
  198.     case GSDLL_DEVICE:
  199.         if (count) {
  200.         sprintf(buf, "mswindll device not supported in this version of Ghostscript\n");
  201.         fputs(buf, stdout);
  202.         }
  203.         break;
  204.     case GSDLL_SYNC:
  205.         break;
  206.     case GSDLL_PAGE:
  207.         break;
  208.     case GSDLL_SIZE:
  209.         break;
  210.     default:
  211.         sprintf(buf,"Callback: Unknown message=%d\n",message);
  212.         fputs(buf, stdout);
  213.         break;
  214.     }
  215.     return 0;
  216. }
  217.  
  218.  
  219.